package com.baizhi.lfk.service;

import com.baizhi.lfk.dao.CategoryMapper;
import com.baizhi.lfk.entity.Category;
import com.baizhi.lfk.entity.CategoryExample;
import com.baizhi.lfk.vo.CategoryVO;
import com.baizhi.lfk.vo.PageVO;
import com.baizhi.lfk.vo.ResultVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryDAO;
    @Override
    public CategoryVO queryOneCategoryPage(PageVO pageVO) {
        //查询所有一级类别，条件：levels=1
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        //分页
        RowBounds rowBounds = new RowBounds((pageVO.getPage()-1)*pageVO.getPageSize(),pageVO.getPageSize());
        List<Category> categories = categoryDAO.selectByExampleAndRowBounds(categoryExample, rowBounds);
        //查询总条数    条件:levels=1
        int total = categoryDAO.selectCountByExample(categoryExample);
        //封装对象
        CategoryVO categoryVO = new CategoryVO();
        categoryVO.setRows(categories);  //一级类别的集合
        categoryVO.setPage(pageVO.getPage()); //当前页
        categoryVO.setTotal(total); //总条数
        //返回
        return categoryVO;
    }
    //添加类别（一级&二级）
    @Override
    public ResultVO registCategory(Category category) {
        //返回数据
        ResultVO resultVO = new ResultVO();

        //判断当前添加的是几级类别
        try {
            if (category.getParentId()==null) {
                //添加一级类别
                category.setLevels(1);
                categoryDAO.insertSelective(category);
                resultVO.setMessage("添加一级类别成功！");
                resultVO.setStatus("200");
            }else{
                //添加二级类别
                category.setLevels(2);
                categoryDAO.insertSelective(category);
                resultVO.setMessage("添加二级类别成功！");
                resultVO.setStatus("200");
            }
        } catch (Exception e) {
            resultVO.setMessage("添加类别失败！");
            resultVO.setStatus("400");

        }
        return resultVO;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Category queryById(Integer id) {
        Category category = new Category();
        category.setId(id);

        return  categoryDAO.selectByPrimaryKey(category);
    }

    @Override
    public ResultVO modifyCategory(Category category) {
        //可以判断一级二级类别  category.getParentId() == null   防止有其他修改操作  单纯只改名字
        ResultVO resultVO = new ResultVO();
        try {
            categoryDAO.updateByPrimaryKeySelective(category);
            resultVO.setMessage("修改成功！");
            resultVO.setStatus("200");
        } catch (Exception e) {
            resultVO.setMessage("修改失败！");
            resultVO.setStatus("400");
            e.printStackTrace();
        }
        return resultVO;
    }

    @Override
    public ResultVO removeCategory(Category category) {
        ResultVO resultVO = new ResultVO();


        //判断是一级类别还是二级类别
        if (category.getParentId()==null) {
            //一级类别 判断是否有二级类别


            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(category.getId());
            //查询当前一级类别下的二级类别
            int total = categoryDAO.selectCountByExample(categoryExample);

            if(total == 0){
                //没有二级类别  删除
                categoryDAO.delete(category);
                resultVO.setMessage("删除一级类别成功！");
                resultVO.setStatus("200");
            }else{
                //存在二级类别  不能删除
                resultVO.setMessage("删除一级类别失败！其下存在子类别");
                resultVO.setStatus("400");
            }
        }else{
            //二级类别  判断是否有视频
            //根据当前类别id(二级类别)查询视频数量
            //数量==0 二级类别下没有视频  可以删除
            categoryDAO.delete(category);
            //resultVO.setMessage("删除二级类别成功！");
            //resultVO.setStatus("200");

            //数量!=  二级类别下存在视频  不能删除
            //resultVO.setMessage("删除二级类别失败！其下存在视频");
            //resultVO.setStatus("400");
        }
        return resultVO;
    }

    //查询某个一级类别下所有的二级类别 分页
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CategoryVO queryTwoCategoryPage(PageVO pageVO) {
        //查询一级类别下所有的二级类别  条件:parentid=传过来的父类别id
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(pageVO.getCategoryId());
        //分页
        RowBounds rowBounds = new RowBounds((pageVO.getPage()-1)*pageVO.getPageSize(),pageVO.getPageSize());

        List<Category> categories = categoryDAO.selectByExampleAndRowBounds(categoryExample, rowBounds);

        //查询总条数    条件:levels=1
        int total = categoryDAO.selectCountByExample(categoryExample);


        //封装对象
        CategoryVO categoryVO = new CategoryVO();
        categoryVO.setRows(categories);  //二级类别的集合
        categoryVO.setPage(pageVO.getPage()); //当前页
        categoryVO.setTotal(total); //总条数
        //返回
        return categoryVO;
    }

    //查询所有一级类别
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Category> queryAllOneCategory() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);

        return categoryDAO.selectByExample(categoryExample);
    }
}
